/**
 * weapp-js v1.7.1(2021-4-16)
 * https://codeup.aliyun.com/motigo/motigo/weapp_base
 * Copyright@ 2021 Motigo, Inc.
 * 
 */
import request from './request.js';
import session from './session.js';
import ui from './ui.js';
import ad from './ad.js';
import payment from './payment.js'
import base_request from './base_request.js';
import theme from './theme.js';
import audio from './audio.js';
import preload from './preload.js';
import file_upload from './file_upload.js';
import recorder from "./recorder.js";
import log from "./log.js";
import util from "./utils/util.js";
import Monitor from './monitor.js';

export default {
    __ready: false,
    appid: '',
    init(login) {
        login = (login != undefined) ? login : true;

        const accountInfo = wx.getAccountInfoSync();
        this.appid = accountInfo.miniProgram.appId;
        Monitor.init({
            appid: this.appid
        });
        base_request.appid = this.appid;

        return new Promise((resolve, reject) => {
            session.init(this.appid, login).then(data => {
                request.init(session.id, this.appid, session.user.unionid, session.user.openid);
                resolve(data);
            }, err => {
                reject(err);
            })
        })
    },
    session: session,
    user: session.user,
    request: request,
    payment: payment,
    showModal: ui.showModal.bind(ui),
    showLoading: ui.showLoading.bind(ui),
    hideLoading: ui.hideLoading.bind(ui),
    previewImage: ui.previewImage.bind(ui),
    login: session.login.bind(session),
    checkAppUpdateAndApply:util.checkAppUpdateAndApply.bind(util),
    theme: theme,
    audio: audio,
    preload: preload,
    recorder:recorder,
    log:log,
    handle_ad(item) {
        ad.handle(item);
    },
    getPhone: session.decrpytUserPhone.bind(session),
    stat: Monitor.stat.bind(Monitor),
    saveNetFile(url) {
        return new Promise((resolve, reject) => {
            wx.downloadFile({
                url: url,
                success: res => {
                    if (res.statusCode === 200) {
                        wx.saveVideoToPhotosAlbum({
                            filePath: res.tempFilePath,
                            success: res => {
                                resolve(res.savedFilePath);
                            },
                            fail: err => {
                                reject('save file fail! err:', err);
                            }
                        })
                    } else {
                        reject('download fail, status:' + res.statusCode);
                    }
                },
                fail: (err) => {
                    console.error('download file fail! url:' + url + ',err:', err);
                    reject('download fail, err:' + err);
                }
            })
        });
    },
    chooseImage(opts) {
        return file_upload.chooseImage(opts);
    },
    uploadFile(opts) {
        return file_upload.uploadFile(opts);
    },
    uploadFiles(opts) {
        return file_upload.uploadFiles(opts);
    },
    // 请求订阅消息
    requestSubscribeMessage(opts) {
        return new Promise(resolve => {
            wx.requestSubscribeMessage({
                tmplIds: opts.tmplIds,
                success: res => {
                    resolve(res);
                },
                fail: () => {
                    resolve(false);
                }
            })
        });
    }
}
